/********************************************************************************
* File Name:	houserenter.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "houserenter.h"
#include "ui_houserenter.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include "databaseconnection.h"
#include <QDebug>
#include <QDateTime>

houserenter::houserenter(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::houserenter)
{
    ui->setupUi(this);
    ui->delivein->setDateTime(QDateTime::currentDateTime());
}

houserenter::~houserenter()
{
    delete ui;
}

void houserenter::on_btnreset_clicked()
{
    this->close();
}

void houserenter::on_btnsave_clicked()
{
    QString roomId = ui->lehouseid->text().trimmed();
    QString rentId =ui->lerentid->text().trimmed();
    QString name = ui->lename->text().trimmed();
    QString contractId = "";
    QString idCard = ui->leidcard->text().trimmed();
    QString place = ui->lenative->text().trimmed();
    QString remark = ui->lenote->text().trimmed();
    QString sex = ui->cbosex->currentText();
    QDateTime inDate = ui->delivein->dateTime();
    QDateTime leftDate = ui->depurchase->dateTime();
    QString price ="";

    int ratingNum;
    int realNum ;

    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        if(roomId==""||rentId==""||name==""||idCard=="")
        {
            QMessageBox::about(this,"Message","Items with * can't not be empty!");
            return;
        }
        QSqlQuery query;
        //是否存在方房间
        query.exec("select count(*) from roomInfo where roomId='"+roomId+"'");
        int flg=0;
        if(query.next())
        {
            flg=query.value(0).toInt();
        }
        if(flg==0)
        {
            QMessageBox::about(this,"Message","No this room,check again!");
            databaseconnection::Close();
            this->close();
            return;
        }
        //判断是否己存在这个ｃｕｓｔｏｍｅｒＩｄ
        query.exec("select count(*) from house.customer where customerId='"+rentId+"'");
        if(query.next())
        {
            if(query.value(0).toInt()>0)
            {
               QString maxNum ;
                query.exec("select customerId from customer order by customerId desc");
                if(query.next())
                {
                    maxNum=query.value(0).toString();
                }
                QMessageBox::about(this,"Message","customerId existed,input again! max id is"+maxNum);
                return ;
            }
        }
        //判断该房间是否还有位置
        query.exec("select ratingNum,realNum from house.roomInfo where roomId='"+roomId+"'");
        if(query.next())
        {
             ratingNum=query.value(0).toInt();
             realNum = query.value(1).toInt();
            if(realNum>=ratingNum)
            {
                QMessageBox::about(this,"Message","This room is full,please check again!");
                databaseconnection::Close();
                this->close();
                return;
            }
        }
        //该房间可用 插入数据,先查询出租金和合同号
        query.exec("select renterId from house.roomInfo where roomId='"+roomId+"'");
        if(query.next())
        {
            QString houseOwnerId =query.value(0).toString();
            query.exec("select Bargain from house.renter where renterId='"+houseOwnerId+"'");
            if(query.next())
            {
                contractId=query.value(0).toString();
            }
        }
        query.exec("select price from house.roomInfo where roomId='"+roomId+"'");
        if(query.next())
        {
            price=query.value(0).toString();//租金
        }
        //sex ="nv";

        //query.exec("insert into house.customer values('"+rentId+"','"+name+"','"+sex+"','"+place+"','"+idCard+"','"+roomId+"','"+inDate+"','"+leftDate+"','"+contractId+"','"+price+"','"+remark+"')");
        query.prepare(tr("INSERT INTO customer VALUES (:rentId, :name,:sex,:place,:idCard,:roomId,:inDate,:leftDate,:contractId,:price,:remark)"));
        query.bindValue(":rentId", rentId);
        query.bindValue(":name", name);
        query.bindValue(":sex", sex);
        query.bindValue(":place", place);
        query.bindValue(":idCard", idCard);
        query.bindValue(":roomId", roomId);
        query.bindValue(":inDate", inDate);
        query.bindValue(":leftDate", leftDate);
        query.bindValue(":contractId", contractId);
        query.bindValue(":price", price);
        query.bindValue(":remark", remark);
        query.exec();

        //query.exec("insert into house.customer values('"+rentId+"','"+name+"','"+sex+"','"+place+"','"+idCard+"','"+roomId+"','"+inDate+"','"+leftDate+"','"+contractId+"','"+price+"','"+remark+"')");
        realNum = realNum+1;
        qDebug()<<sex;

        QString sRealNum = QString::number(realNum,10);
        query.exec("update roomInfo set realNum='"+sRealNum+"' where roomId='"+roomId+"'");
        QMessageBox::about(this,"Message","Youre welcome!");
        databaseconnection::Close();
        this->close();
    }
}
